package com.junzhiit.nengji.cms.service.impl;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.bo.CouponsBO;
import com.junzhiit.nengji.bo.UserBO;
import com.junzhiit.nengji.bo.UserCouponsBO;
import com.junzhiit.nengji.cms.service.UserCouponsService;
import com.junzhiit.nengji.cms.web.exception.CouponsNotFoundException;
import com.junzhiit.nengji.cms.web.exception.UserNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.CouponsVO;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.UserCouponsAddVO;
import com.junzhiit.nengji.dao.CouponsDAO;
import com.junzhiit.nengji.dao.UserCouponsDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.model.Coupons;
import com.junzhiit.nengji.model.UserCoupons;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.Status;

@Service
public class UserCouponsServiceImpl extends BaseServiceImpl  implements UserCouponsService{
	
	@Autowired
	private UserCouponsDAO userCouponsDAO;
	
	@Autowired
	private CouponsDAO couponsDAO;
	
	@Autowired
	private UserDAO userDAO;

	@Override
	public Map<String, Object> getCouponsAll(DataTablesParamVO dtp,CouponsVO couponsVO) {
		CouponsBO couponsBO = new CouponsBO();
		BeanUtils.copyProperties(couponsVO, couponsBO);
		List<UserCouponsBO> list = userCouponsDAO.getCouponsAll(couponsBO, dtp.getStart(), dtp.getLength());
		int totalCount = userCouponsDAO.getCouponsCount(couponsBO);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO addCoupons(UserCouponsAddVO userCouponsAddVO) {
		ResultVO result=new ResultVO();
		UserCoupons userCoupons=new UserCoupons();
		BeanUtils.copyProperties(userCouponsAddVO, userCoupons);
		
		UserBO user=userDAO.getUserById(userCoupons.getUserId());
		if (user == null) {
			throw new UserNotFoundException("该用户未找到");
		}
		
		Coupons coupons = couponsDAO.query(userCoupons.getCouponsId());
		
		if (coupons == null) {
			throw new CouponsNotFoundException("优惠卷未找到");
		}
		
		BeanUtils.copyProperties(coupons, userCoupons);
		
		if(coupons.getValidTimeState()==0){
			userCoupons.setStartTime(coupons.getValidStartTime());
			userCoupons.setEndTime(coupons.getValidEndTime());
		}else{
			Date now=new Date();
			Date date=DateUtil.addDays(now, coupons.getValidDays());
			userCoupons.setStartTime(now);
			userCoupons.setEndTime(date);
		}
		userCoupons.setStatus(Is.NO.toIntValue());
		userCoupons.setUserId(userCoupons.getUserId());
		userCoupons.setShopId(coupons.getShopId());
		userCoupons.setCouponsName(coupons.getName());
		userCoupons.setCouponsId(userCouponsAddVO.getCouponsId());
		userCoupons.setCouponsType(coupons.getType());
		if(coupons.getType()==0){
			//等于1为随机卷
			if(coupons.getIsOpenRandom()==1){
				//在条件为0.6,3的10万次测试中取Max、Min的比例为0.053%，在1-100区间内大于60的占30%
				Double offer =  this.randomWeights(coupons.getStartPrice(),coupons.getEndPrice(), 0.6,2);
				userCoupons.setPrice(offer);
			}else{
				userCoupons.setPrice(coupons.getPrice());
			}
		}
		
		UserCoupons  data=userCouponsDAO.insert(userCoupons);
		result.setData(data);
		return result;
	}

	@Override
	public void soldOutUserCoupons() {
		Date now = new Date();
		List<UserCoupons> userCoupons = userCouponsDAO.getCouponsById();
		for(UserCoupons coupons : userCoupons){
			if(coupons.getEndTime().getTime() <= now.getTime()){
				coupons.setStatus(Status.INACTIVE.toIntStatus());
				userCouponsDAO.update(coupons);
			}
		}
	}
	
	/**
	 * maxProportion：代表权重衡量的区间段的大小,取值为(0.1~0.9),值越大代表权重区间值越小即越接近于Max
	 * weights：权重,取值为(1~9),值越大代表在权重区间取值的比重越大
	 * */
	private Double randomWeights(Double min,Double max,Double maxProportion,int weights){
		weights = weights>=10 && weights<0 ?1:weights;
		maxProportion = maxProportion>=1 && maxProportion<0 ?0.1:maxProportion;
		Double median = (maxProportion*(max-min))+min;
		Double result = new Double(0);
		List<Double> list = new ArrayList<Double>();
		for(int i = 1;i<=10;i++){
			if(i<=weights){
				list.add( (Math.random()*(max-median))+median);
			}else{
				list.add( (Math.random()*(median-min))+min);
			}
		}
		result = list.get((int)(1+Math.random()*(10-1)));
		return Double.parseDouble(new DecimalFormat("#.##").format(result));
	}

	@Override
	public ResultVO delCoupons(String couponsId) {
		ResultVO result = new ResultVO();
		Coupons coupons = couponsDAO.query(couponsId);
		if (coupons == null) {
			throw new CouponsNotFoundException("优惠卷未找到");
		}
		boolean data = couponsDAO.updateToDelete(couponsId);
		result.setData(data);
		return result;
	}
}
